<?php

namespace addons\Minerva\backend\controllers;

use addons\Minerva\common\models\AllowExt;
use addons\Minerva\common\models\AllowHeader;
use addons\Minerva\common\models\Nets;
use addons\Minerva\common\models\NotallowExt;
use addons\Minerva\common\models\NotallowHeader;
use Yii;
use common\helpers\ArrayHelper;
use common\traits\AuthRoleTrait;
use common\models\rbac\AuthRole;
use common\enums\AppEnum;
use yii\helpers\Json;

/**
 * Class AuthRoleController
 * @package backend\modules\base\controllers
 * @author jianyan74 <751393839@qq.com>
 */
class AuthRoleController extends BaseController
{
    use AuthRoleTrait;

    /**
     * @var AuthRole
     */
    public $modelClass = AuthRole::class;

    /**
     * 默认应用
     *
     * @var string
     */
    public $appId = AppEnum::BACKEND;

    /**
     * 权限来源
     *
     * false:所有权限，true：当前角色
     *
     * @var bool
     */
    public $sourceAuthChild = true;

    /**
     * 渲染视图前缀
     *
     * @var string
     */
    public $viewPrefix = '@addons/Minerva/backend/views/auth-role/';

    public function actionFileAuth(){
        $role_id = Yii::$app->request->get('id', null);
//        $model = $this->findModel($role_id);
//        $transaction = Yii::$app->db->beginTransaction();
//        try{
//            $post = Yii::$app->request->post();
            $nets = new Nets();

            $allow_ext = new AllowExt();

            $allow_header = new AllowHeader();

            $notallow_ext = new NotallowExt();

            $notallow_header = new NotallowHeader();

//            $nets = $nets->
//            $nets->load($post);
//            echo '<pre>';
//            var_dump($model);die;
//            $transaction->commit();
//        }catch (\Exception $e){
//            $transaction->rollBack();
//
//        }

//        $model->pid = Yii::$app->request->get('pid', null) ?? $model->pid; // 父id
//        $model->level = Yii::$app->request->get('level')+1;
//        if ($model->load(Yii::$app->request->post()) && $model->save()) {
//            return $this->referrer();
//        }
//        $one_level = $model::find()->asArray()->all();
//        $list = ArrayHelper::removeByValue($one_level, $id);
//        $models = ArrayHelper::itemsMerge($list);
//        $data = ArrayHelper::map(ArrayHelper::itemsMergeDropDown($models), 'id', 'title');
//        $dropDownList = ArrayHelper::merge([0 => '顶级部门'], $data);

        return $this->render($this->action->id, [
//            'model' => $model,
            'nets' => $nets,
            'allow_ext' => $allow_ext,
            'notallow_ext' => $notallow_ext,
            'allow_header' => $allow_header,
            'notallow_header' => $notallow_header
//            'dropDownList' => $dropDownList
        ]);
    }

    public function actionAjaxEdit(){
        $model = new AllowExt();
        $model = $model::findOne(['id' => 1]);
        $content = Json::decode($model->content);
        $model->content = $content;
        if(!$this->layout){
            var_dump(666);
        }
        return $this->renderAjax($this->action->id,[
            'model' => $model
        ]);
    }
}